class Fluctuation < ApplicationRecord
  def self.ransackable_attributes(auth_object = nil)
    ["code", "created_at", "href", "id", "id_value", "name", "title", "trend", "updated_at"]
  end

  scope :surge, -> { where(trend: '大涨').where('created_at >= ?', Time.current.beginning_of_day) }
  scope :surge_stop, -> { where(trend: '涨停').where('created_at >= ?', Time.current.beginning_of_day)  }
  scope :plunge, -> { where(trend: '大跌').where('created_at >= ?', Time.current.beginning_of_day) }
  scope :plunge_stop, -> { where(trend: '跌停').where('created_at >= ?', Time.current.beginning_of_day)  }

  after_create :send_dingding_notification

  def send_dingding_notification
    stock = Stock.find_by_symbol self.code
    content = <<~MD
      **#{self.title}**
      > 名称: #{self.name}        
      > [查看详情](#{self.href})  
      > [show](https://wyyzrx.com/admin/stocks/#{stock.id}) 
      > 时间: #{self.created_at.strftime("%F %T")}
    MD
    Time.now.day > 15 ? (Robot.dd_fluctuation_ai(content)) : (Robot.dd_fluctuation(content))

  end
end
